//
//  Labs_MM_Swift_COW_DemoVC.swift
//  APSwift
//
//  Created by jianbo.pei on 2023/8/21.
//

/**
 **值类型内存管理**：值类型（如结构体、枚举）在赋值或传递参数时会进行复制。`Swift` 采用了 `Copy-On-Write`（`COW`，写时复制）优化策略，只有当值类型需要被修改时，`Swift` 才会进行实际的复制操作。值类型通常存储在栈上，其内存管理由编译器处理。当值类型的变量超出其作用域时，内存会自动释放。
 */

import UIKit

struct COW_Dog {
    var name: String
}

class Labs_MM_Swift_COW_DemoVC: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        test()
    }
    
    private func test() {
        var dog1 = COW_Dog(name: "a huang")
        var dog2 = dog1
        
        print("111 dog1 is \(dog1), dog1'name is \(dog1.name)")
        print("111 dog2 is \(dog2), dog2'name is \(dog2.name)")
        
        dog2.name = "xiao huang"
        print("222 dog1 is \(dog1), dog1'name is \(dog1.name)")
        print("222 dog2 is \(dog2), dog2'name is \(dog2.name)")
    }

}
